home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c14 / Blocking.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  8.9 KB  |  328 lines

  1. //: Blocking.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Demonstrates the various ways a thread
  50. // can be blocked.
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.applet.*;
  54. import java.io.*;
  55.  
  56. //////////// The basic framework ///////////
  57. class Blockable extends Thread {
  58.   private Peeker peeker;
  59.   protected TextField state = new TextField(40);
  60.   protected int i;
  61.   public Blockable(Container c) {
  62.     c.add(state);
  63.     peeker = new Peeker(this, c);
  64.   }
  65.   public synchronized int read() { return i; }
  66.   protected synchronized void update() {
  67.     state.setText(getClass().getName()
  68.       + " state: i = " + i);
  69.   }
  70.   public void stopPeeker() { 
  71.     // peeker.stop(); Deprecated in Java 1.2
  72.     peeker.terminate(); // The preferred approach
  73.   }
  74. }
  75.  
  76. class Peeker extends Thread {
  77.   private Blockable b;
  78.   private int session;
  79.   private TextField status = new TextField(40);
  80.   private boolean stop = false;
  81.   public Peeker(Blockable b, Container c) {
  82.     c.add(status);
  83.     this.b = b;
  84.     start();
  85.   }
  86.   public void terminate() { stop = true; }
  87.   public void run() {
  88.     while (!stop) {
  89.       status.setText(b.getClass().getName()
  90.         + " Peeker " + (++session)
  91.         + "; value = " + b.read());
  92.        try {
  93.         sleep(100);
  94.       } catch (InterruptedException e){}
  95.     }
  96.   }
  97. } ///:Continued
  98. ///:Continuing
  99. ///////////// Blocking via sleep() ///////////
  100. class Sleeper1 extends Blockable {
  101.   public Sleeper1(Container c) { super(c); }
  102.   public synchronized void run() {
  103.     while(true) {
  104.       i++;
  105.       update();
  106.        try {
  107.         sleep(1000);
  108.       } catch (InterruptedException e){}
  109.     }
  110.   }
  111. }
  112.   
  113. class Sleeper2 extends Blockable {
  114.   public Sleeper2(Container c) { super(c); }
  115.   public void run() {
  116.     while(true) {
  117.       change();
  118.        try {
  119.         sleep(1000);
  120.       } catch (InterruptedException e){}
  121.     }
  122.   }
  123.   public synchronized void change() {
  124.       i++;
  125.       update();
  126.   }
  127. } ///:Continued
  128. ///:Continuing
  129. /////////// Blocking via suspend() ///////////
  130. class SuspendResume extends Blockable {
  131.   public SuspendResume(Container c) {
  132.     super(c);    
  133.     new Resumer(this); 
  134.   }
  135. }
  136.  
  137. class SuspendResume1 extends SuspendResume {
  138.   public SuspendResume1(Container c) { super(c);}
  139.   public synchronized void run() {
  140.     while(true) {
  141.       i++;
  142.       update();
  143.       suspend(); // Deprecated in Java 1.2
  144.     }
  145.   }
  146. }
  147.  
  148. class SuspendResume2 extends SuspendResume {
  149.   public SuspendResume2(Container c) { super(c);}
  150.   public void run() {
  151.     while(true) {
  152.       change();
  153.       suspend(); // Deprecated in Java 1.2
  154.     }
  155.   }
  156.   public synchronized void change() {
  157.       i++;
  158.       update();
  159.   }
  160. }
  161.  
  162. class Resumer extends Thread {
  163.   private SuspendResume sr;
  164.   public Resumer(SuspendResume sr) {
  165.     this.sr = sr;
  166.     start();
  167.   }
  168.   public void run() {
  169.     while(true) {
  170.        try {
  171.         sleep(1000);
  172.       } catch (InterruptedException e){}
  173.       sr.resume(); // Deprecated in Java 1.2
  174.     }
  175.   }
  176. } ///:Continued
  177. ///:Continuing
  178. /////////// Blocking via wait() ///////////
  179. class WaitNotify1 extends Blockable {
  180.   public WaitNotify1(Container c) { super(c); }
  181.   public synchronized void run() {
  182.     while(true) {
  183.       i++;
  184.       update();
  185.        try {
  186.         wait(1000);
  187.       } catch (InterruptedException e){}
  188.     }
  189.   }
  190. }
  191.  
  192. class WaitNotify2 extends Blockable {
  193.   public WaitNotify2(Container c) {
  194.     super(c);
  195.     new Notifier(this); 
  196.   }
  197.   public synchronized void run() {
  198.     while(true) {
  199.       i++;
  200.       update();
  201.        try {
  202.         wait();
  203.       } catch (InterruptedException e){}
  204.     }
  205.   }
  206. }
  207.  
  208. class Notifier extends Thread {
  209.   private WaitNotify2 wn2;
  210.   public Notifier(WaitNotify2 wn2) {
  211.     this.wn2 = wn2;
  212.     start();
  213.   }
  214.   public void run() {
  215.     while(true) {
  216.        try {
  217.         sleep(2000);
  218.       } catch (InterruptedException e){}
  219.       synchronized(wn2) {
  220.         wn2.notify();
  221.       }
  222.     }
  223.   }
  224. } ///:Continued
  225. ///:Continuing
  226. class Sender extends Blockable { // send
  227.   private Writer out;
  228.   public Sender(Container c, Writer out) { 
  229.     super(c);
  230.     this.out = out; 
  231.   }
  232.   public void run() {
  233.     while(true) {
  234.       for(char c = 'A'; c <= 'z'; c++) {
  235.          try {
  236.           i++;
  237.           out.write(c);
  238.           state.setText("Sender sent: " 
  239.             + (char)c);
  240.           sleep((int)(3000 * Math.random()));
  241.         } catch (InterruptedException e){}
  242.           catch (IOException e) {}
  243.       }
  244.     }
  245.   }
  246. }
  247.  
  248. class Receiver extends Blockable {
  249.   private Reader in;
  250.   public Receiver(Container c, Reader in) { 
  251.     super(c);
  252.     this.in = in; 
  253.   }
  254.   public void run() {
  255.     try {
  256.       while(true) {
  257.         i++; // Show peeker it's alive
  258.         // Blocks until characters are there:
  259.         state.setText("Receiver read: "
  260.           + (char)in.read());
  261.       }
  262.     } catch(IOException e) { e.printStackTrace();}
  263.   }
  264. } ///:Continued
  265. ///:Continuing
  266. /////////// Testing Everything ///////////
  267. public class Blocking extends Applet {
  268.   private Button 
  269.     start = new Button("Start"),
  270.     stopPeekers = new Button("Stop Peekers");
  271.   private boolean started = false;
  272.   private Blockable[] b;
  273.   private PipedWriter out;
  274.   private PipedReader in;
  275.   public void init() {
  276.      out = new PipedWriter();
  277.     try {
  278.       in = new PipedReader(out);
  279.     } catch(IOException e) {}
  280.     b = new Blockable[] {
  281.       new Sleeper1(this),
  282.       new Sleeper2(this),
  283.       new SuspendResume1(this),
  284.       new SuspendResume2(this),
  285.       new WaitNotify1(this),
  286.       new WaitNotify2(this),
  287.       new Sender(this, out),
  288.       new Receiver(this, in)
  289.     };
  290.     start.addActionListener(new StartL());
  291.     add(start);
  292.     stopPeekers.addActionListener(
  293.       new StopPeekersL());
  294.     add(stopPeekers);
  295.   }
  296.   class StartL implements ActionListener {
  297.     public void actionPerformed(ActionEvent e) {
  298.       if(!started) {
  299.         started = true;
  300.         for(int i = 0; i < b.length; i++)
  301.           b[i].start();
  302.       }
  303.     }
  304.   }
  305.   class StopPeekersL implements ActionListener {
  306.     public void actionPerformed(ActionEvent e) {
  307.       // Demonstration of the preferred 
  308.       // alternative to Thread.stop():
  309.       for(int i = 0; i < b.length; i++)
  310.         b[i].stopPeeker();
  311.     }
  312.   }
  313.   public static void main(String[] args) {
  314.     Blocking applet = new Blocking();
  315.     Frame aFrame = new Frame("Blocking");
  316.     aFrame.addWindowListener(
  317.       new WindowAdapter() {
  318.         public void windowClosing(WindowEvent e) {
  319.           System.exit(0);
  320.         }
  321.       });
  322.     aFrame.add(applet, BorderLayout.CENTER);
  323.     aFrame.setSize(350,550);
  324.     applet.init();
  325.     applet.start();
  326.     aFrame.setVisible(true);
  327.   }
  328. } ///:~